package com.cash.app.ui.other

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import com.cash.app.R
import com.cash.app.base.BaseActivity
import com.cash.app.databinding.AcRepayContainerBinding

class RepayActivity : BaseActivity() {

    private lateinit var binding: AcRepayContainerBinding
    private var repayUrl: String = ""

    companion object {
        private const val EXTRA_REPAY_URL = "extra_repay_url"

        fun start(context: Context, repayUrl: String) {
            val intent = Intent(context, RepayActivity::class.java)
            intent.putExtra(EXTRA_REPAY_URL, repayUrl)
            context.startActivity(intent)
        }
    }

    override fun setViewBind() {
        binding = AcRepayContainerBinding.inflate(layoutInflater)
    }

    override fun getLayoutView(): View {
        return binding.root
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        repayUrl = intent.getStringExtra(EXTRA_REPAY_URL) ?: ""
        if (repayUrl.isNotEmpty()) {
            initView()
        } else {
            Toast.makeText(this, getString(R.string.repay_url_error), Toast.LENGTH_SHORT).show()
        }
    }

    private fun initView() {
        binding.webView.apply {
            settings.javaScriptEnabled = true
            settings.domStorageEnabled = true
            webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                    view.loadUrl(url)
                    return true
                }
            }
            loadUrl(repayUrl)
        }
    }

    override fun onDestroy() {
        binding.webView.destroy()
        super.onDestroy()
    }
} 